<?php

namespace app\reserve\validate;

use think\Db;
use think\Validate;

/**
 * Class AdminGroup
 * @package app\reserve\validate
 * @author zhenhong~
 * 权限组验证器
 */
class AdminGroup extends Validate
{
    protected $rule = [
        'group_name' => 'require|checkGroupUnique',
        'permission' => 'require'
    ];

    protected $message = [
        'group_name.require' => '请输入分组名称',
        'group_name.checkGroupUnique' => '该分组名称已经存在',
        'permission' => '请选择权限'
    ];

    /**
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * 验证分组名称的唯一性
     * zhenhong~
     */
    protected function checkGroupUnique($value,$rule,$data){
        if(array_key_exists('group_id',$data)){
            $where = [
                'group_id' => ['neq',$data['group_id']],
                'group_name' => $value,
                'status' => 0
            ];
            $name = Db::name('AdminGroup')->where($where)->value('group_name');
            return $name ? false : true;
        }else{
            $where = [
                'group_name' => $value,
                'status' => 0
            ];
            $name = Db::name('AdminGroup')->where($where)->value('group_name');
            return $name ? false : true;
        }
    }

}